package com.ctshk.rpc.hotel.APIUtils;

import com.ctshk.rpc.hotel.dto.HotelCityDTO;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 龙腾静态数据请求get方法
 * */
public class GetApiUtils {
    public static List<HotelCityDTO> downloadFile(String urlPath) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = urlPath;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            List<HotelCityDTO> hotelCityDTOS = new ArrayList<>();
            while ((line = in.readLine()) != null) {
                HotelCityDTO hotelCityDTO = new HotelCityDTO();
                line.replace("&lt;", "");
                line.replace("&gt;","");
                line.replace("&quot;","");
                if(line.contains("Country") && !line.contains("Countrys") && !line.contains("/Country")){
                    String[] split = line.split("=");
                    for(int i = 0 ; i< split.length; i++){
                        if(i == 1){
                            String cityId = split[i].replace("CountryName", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityId(cityId.replace(" ",""));
                        }
                        if(i == 2){
                            String cityName = split[i].replace("SpellNameShort", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityName(cityName.replace(" ",""));
                            hotelCityDTO.setCityLevel(1);
                        }
                    }
                    hotelCityDTOS.add(hotelCityDTO);
                }
                if(line.contains("Province") && !line.contains("/Province")){
                    String[] split = line.split("=");
                    for(int i = 0 ; i< split.length; i++){
                        if(i == 1){
                            String cityId = split[i].replace("ProvinceName", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityId(cityId.replace(" ",""));
                        }
                        if(i == 2){
                            String cityName = split[i].replace("SpellNameShort", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityName(cityName.replace(" ",""));
                            hotelCityDTO.setCityLevel(2);
                        }
                    }
                    hotelCityDTOS.add(hotelCityDTO);
                }
                if(line.contains("City") && !line.contains("post") && !line.contains("CityQuery")){
                    String[] split = line.split("=");
                    for(int i = 0 ; i< split.length; i++){
                        if(i == 1){
                            String cityId = split[i].replace("CityName", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityId(cityId.replace(" ",""));
                        }
                        if(i == 2){
                            String cityName = split[i].replace("SpellNameShort", "").replace("&quot;","").replace("&gt;","").replace("/","");
                            hotelCityDTO.setCityName(cityName.replace(" ",""));
                            hotelCityDTO.setCityLevel(3);
                        }
                    }
                    hotelCityDTOS.add(hotelCityDTO);
                }

                //result += line;
                //System.out.println(line);

            }
            return hotelCityDTOS;
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        List<HotelCityDTO> hotelCityDTOList = downloadFile("http://202.104.101.32:8077/StaticInfoQuery.aspx?ActionName=CityQuery&auth=RU4wMDAwMDFFMTBBREMzOTQ5QkE1OUFCQkU1NkUwNTdGMjBGODgzRTM2OWI0NjljLTUxYjItNDNjZC05Njc3LTkzNGNhMTdmMjY1MVJVNHdNREF3TURGRk1UQkJSRU16T1RRNVFrRTFPVUZDUWtVMU5rVXdOVGRHTWpCR09EZ3pSVE0yT1dJME5qbGpMVFV4WWpJdE5ETmpaQzA1TmpjM0xUa3pOR05oTVRkbU1qWTFNUT09Cg==");
        System.out.println(hotelCityDTOList);
    }
}
